/**
 * @author janet
 * @date 2015-4-2
 */
package net.oscorner.controller.home;

import java.awt.Menu;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.oscorner.commons.BaseEntity;
import net.oscorner.commons.SpringsUtils;
import net.oscorner.entiry.Menus;
import net.oscorner.entiry.User;
import net.oscorner.services.menu.MenuService;
import net.oscorner.services.user.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.support.RequestContextUtils;


/**
 * @author janet
 * @date 2015-4-2 上午9:28:08
 * 
 */
@Controller
public class HomeController {
	@Autowired(required=true)
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	@RequestMapping("/index")
	public ModelAndView index(ModelAndView index) throws IOException{
		User u = (User)request.getSession().getAttribute("loginedUser");
		List<Menu> ms = new ArrayList<Menu>();
		index.addObject("loginUser", u);
		index.addObject("query",getService().queryUsers(u));
		index.setViewName("common/list");
		index.addObject("list", ms);
		return index;
	}
	@RequestMapping(method=RequestMethod.GET,value="/login")
	public String login(){
		return "login";
	}
	@RequestMapping(method=RequestMethod.POST,value="/login")
	public ModelAndView login(User user){
		UserService u = (UserService)SpringsUtils.getBean("userService");
		MenuService m = (MenuService)SpringsUtils.getBean("menuService");
		boolean flag = u.checkUser(user);
		if(flag){
			Menus e = new Menus();
			e.setType("0");
			List<Menus> menuList = m.queryMenus(e);
			request.getSession().setAttribute("loginedUser", user);
			request.getSession().setAttribute("menuList", menuList);
		}
		ModelAndView index = new ModelAndView("redirect:/index","loginUser",user);
		return index;
	}
	@RequestMapping("/logout")
	public String logout(){
		request.getSession().invalidate();
		return "redirect:login";
	}
	@RequestMapping(value="query")
	public String query(HttpServletResponse response,BaseEntity e,String menuId){
		MenuService menuService = (MenuService)SpringsUtils.getBean("menuService");
		Menus m = menuService.getMenu(menuId);
		request.getSession().setAttribute("currentMenus", m);
		if(m == null){
			return "/index";
		}
		return "forward:"+m.getLinkurl();
	}
	
	private UserService getService(){
		return (UserService)SpringsUtils.getBean("userService");
	}
	
	
}
